package com.wenhai.spring.aop.pointcut;

import com.wenhai.spring.aop.annotation.AdviceRequired;
import org.springframework.aop.Advisor;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;

/**
 * {@link org.springframework.aop.support.annotation.AnnotationMatchingPointcut} 示例
 *
 * @author wenhai
 * @date   2021/5/6
 */
public class AnnotationPointcut {
    public static void main(String[] args) {
        GrammyGuitarist grammyGuitarist = new GrammyGuitarist();
        AnnotationMatchingPointcut pointcut = AnnotationMatchingPointcut.forMethodAnnotation(AdviceRequired.class);
        Advisor advisor = new DefaultPointcutAdvisor(pointcut, new SimpleAdvice());
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setTarget(grammyGuitarist);
        proxyFactory.addAdvisor(advisor);
        GrammyGuitarist proxy = (GrammyGuitarist) proxyFactory.getProxy();
        proxy.rest();
        proxy.sing(new Guitar());
    }
}
